/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.help.web;

import org.crystalrange.BaseController;
import org.crystalrange.help.service.HelpFolderService;
import org.crystalrange.help.service.HelpItemService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Iframe;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;

public class HelpContentController extends BaseController {
    
    protected Tree treeHelp;
    protected HelpFolderService helpFolderService;
    protected HelpItemService helpItemService;
    protected Iframe iframeContent;

    public HelpFolderService getHelpFolderService() {
        return this.helpFolderService;
    }

    public void setHelpFolderService(HelpFolderService helpFolderService) {
        this.helpFolderService = helpFolderService;
    }

    public HelpItemService getHelpItemService() {
        return this.helpItemService;
    }

    public void setHelpItemService(HelpItemService helpItemService) {
        this.helpItemService = helpItemService;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        HelpRootTreeNode helpRootTreeNode = new HelpRootTreeNode();
        helpRootTreeNode.setHelpFolderService(this.helpFolderService);
        helpRootTreeNode.setHelpItemService(this.helpItemService);
        helpRootTreeNode.readChildren();

        this.treeHelp.setModel(new HelpTreeModel(helpRootTreeNode));
        this.treeHelp.setItemRenderer(new HelpTreeRenderer());
    }

    public void onClick$menuitemLogout(Event event) throws Exception {
        this.logout();
    }

    public void onClick$menuitemHelpContents(Event event) throws Exception {
        Clients.evalJavaScript("window.open('~./zul/html/helpContent.zul', '_blank');");
    }

    public void onSelect$treeHelp(SelectEvent event) throws Exception {
        Treeitem treeitem = this.treeHelp.getSelectedItem();
        if (treeitem.getValue() instanceof HelpFolderTreeNode) {
            HelpFolderTreeNode helpFolderTreeNode = (HelpFolderTreeNode) treeitem.getValue();
            this.iframeContent.setSrc(helpFolderTreeNode.getLocation());
        }
        if (treeitem.getValue() instanceof HelpItemTreeNode) {
            HelpItemTreeNode helpItemTreeNode = (HelpItemTreeNode) treeitem.getValue();
            this.iframeContent.setSrc(helpItemTreeNode.getLocation());
        }
    }

    private void logout() throws Exception {
        this.execution.sendRedirect("/j_spring_security_logout");
    }
}
